import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class num290 {
    public static boolean wordPattern(String pattern, String s) {
        String[] str = s.split(" ");
        if(pattern.length() != str.length){
            return false;
        }
        StringBuilder sb1 = new StringBuilder();
        int count = 1;
        Map<String ,Integer> map = new HashMap<>();
        for(String s1 : str){
            if(map.containsKey(s1)){
                sb1.append(map.get(s1));
            }else{
                map.put(s1,count);
                sb1.append(count ++);
            }
        }
        StringBuilder sb2 = new StringBuilder();
        count = 1;
        Map<Character,Integer> map1 = new HashMap<>();
        for (int i = 0; i < pattern.length(); i++) {
            if(map.containsKey(pattern.charAt(i))){
                sb2.append(map1.get(pattern.charAt(i)));
            }else{
                map1.put(pattern.charAt(i),count);
                sb2.append(count ++);
            }
        }
        return sb1.toString().equals(sb2.toString());
//        StringBuilder s1 = new StringBuilder();
//        StringBuilder s2 = new StringBuilder();
//        for (int i = 0; i < pattern.length(); i++) {
//            s1.append(pattern.indexOf(pattern.charAt(i)));
//        }
//        String[] arr = s.split(" ");
//        List<String> list = Arrays.asList(arr);
//        for (String str : arr) {
//            s2.append(list.indexOf(str));
//        }
//        return s1.toString().equals(s2.toString());
    }
}
